package com.yang.library.util;


import com.yang.library.entry.Book;
import com.yang.library.entry.User;

import java.util.List;
import java.util.Scanner;

/**
 * 输出工具类
 *
 * @author Chen
 */
public class PrintUtils {
    private PrintUtils() {
    }

    /**
     * 打印图书列表
     *
     * @param books 图书列表
     */
    public static void printBooks(List<Book> books) {
        System.out.printf("%-5s%-10s%-10s%-10s%-10s%-20s%-10s%-20s%-10s\n", "序号", "书名", "作者", "ISBN", "是否借阅", "借阅日期", "借阅人", "还书日期", "还书人");
        int counter = 1;
        for (Book book : books) {
            System.out.printf("%-5d%-10s%-10s%-10s%-10s%-20s%-10s%-20s%-10s\n", counter, book.getTitle(), book.getAuthor(), book.getIsbn(), book.isBorrowed(), book.getBorrowDate(), book.getBorrower(), book.getReturnDate(), book.getReturner());
            counter++;
        }
    }

    /**
     * 打印用户列表
     *
     * @param users 用户列表
     */
    public static void printUsers(List<User> users) {
        System.out.printf("%-5s%-20s%-20s%-20s\n", "序号", "用户名", "密码", "角色");
        int counter = 1;
        for (User user : users) {
            System.out.printf("%-5d%-20s%-20s%-20s\n", counter, user.getName(), user.getPassword(), user.getRole());
            counter++;
        }
    }

    /**
     * 获取用户输入的整数
     *
     * @param sc     输入流
     * @param prompt 提示信息
     * @return 输入的整数
     */
    public static int getIntInput(Scanner sc, String prompt) {
        while (true) {
            System.out.println(prompt);
            try {
                return sc.nextInt();
            } catch (Exception e) {
                sc.next(); // 清除非法输入
                System.out.println("输入错误，请重新输入！");
            }
        }
    }

    // 封装重复输入逻辑

    /**
     * 封装重复输入逻辑
     *
     * @param prompt 提示信息
     * @return 输入的字符串
     */
    public static String getInput(String prompt) {
        System.out.println(prompt);
        return AppContext.getInstance().getScanner().next();
    }
}
